import {get} from "@/request/useRequest";
import {message, Modal} from "ant-design-vue";
import { CodeProps } from '@/components/typing'

/**
 @Author Zhenglinbin
 @Date 2021/3/11
 **/
export const codeHook = () => {
    const generateCode = (key: string): Promise<CodeProps> => {
        return new Promise(async resolve => {
            const res = await get('serialNumber/generateNumber', {reserved: key})
            if (res.success) {
                if ('需要升位' === res.data?.reserved) {
                    Modal.confirm({
                        title: '编号模版升位',
                        content: '编号位数已达到最高值，是否自动升位？',
                        onOk() {
                            get('serialNumber/upNumber', {labelId: res.data?.labelId}).then(res => {
                                if (res.success) {
                                    message.success("自动升位成功！");
                                    resolve(generateCode(key))
                                }
                            })
                        }
                    });
                } else {
                    resolve({id: res.data?.id,code: res.data?.reserved})
                }
            }
        })
    }
    return {
        generateCode
    }
}
